package de.uni_potsdam.hpi.bpt.ai.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;

import org.json.JSONException;

import de.uni_potsdam.hpi.bpt.ai.collection.BPMAIExport;
import de.uni_potsdam.hpi.bpt.ai.collection.BPMAIExportBuilder;
import de.uni_potsdam.hpi.bpt.ai.collection.Model;
import de.uni_potsdam.hpi.bpt.ai.collection.Revision;
import de.uni_potsdam.hpi.bpt.ai.diagram.Diagram;

public abstract class Experiment {
	final static String NS_BPMN = "http://b3mn.org/stencilset/bpmn2.0#";
	final static String NS_EPC = "http://b3mn.org/stencilset/epc#";
	
	static final String MODEL_PATH = ".." + File.separator + "models" + File.separator + "2011-04-19_signavio_academic_processes";
	//static final String MODEL_PATH = ".." + File.separator + "models" + File.separator + "epc_single";
	static final String STAT_PATH = ".." + File.separator + "models";
	
	// package visibility
	final static String SIZE = "size";
	final static String SIZE_ARCS = "sizeArcs";
	final static String DIAMETER = "diameter";
	final static String DENSITY = "density";
	final static String CNC = "cnc";
	final static String AVG_DC = "avgDc";
	final static String MAX_DC = "maxDc";
	final static String CYC = "cyc";
	final static String TS = "ts";
	final static String CH = "ch";
	
	protected int eachModel() throws UnsupportedEncodingException, FileNotFoundException, JSONException, IOException {
		return eachModel(new HashSet<String>());
	}
	
	protected int eachModel(Set<String> namespaces) throws UnsupportedEncodingException, FileNotFoundException, JSONException, IOException {
	
		File rootDir = new File(MODEL_PATH);
		
		if (!rootDir.exists()) {
			throw new FileNotFoundException("root directory " + MODEL_PATH + " does not exist");
		}

		if (!rootDir.isDirectory()) {
			throw new FileNotFoundException("root directory " + MODEL_PATH + " is not a directory");
		}
		
		// parse directory
		BPMAIExport ex = BPMAIExportBuilder.parseDirectory(rootDir);
		int c = 0;
		int cc = 80;
		
		@SuppressWarnings("unused")
		Set<Model> ms = ex.getModels();
		
		for (Model m : ex.getModels()) {
			Revision last = null;
			for (Revision r : m.getRevisions()) {
				last = r;
			}
			
			Diagram diagram = last.getDiagram();
			
			if (0 == namespaces.size() || (namespaces.contains(diagram.getStencilset().getNamespace()))) {
				c++;
				
//				System.out.print("*");
				if (0 == c % cc) {
					System.out.println();
				}
				
				examineModel(m, diagram, c);
			}
		}
		
		return c;
	}
	
	protected abstract void examineModel(Model model, Diagram diagram, int index);
	
}
